﻿using System;
using System.Net;
using System.Windows.Input;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using Jok.Client.Infrastructure;

namespace Jok.Client.Portal.ViewModels
{
    [Export]
    public class LoginViewModel : ViewModel
    {
        readonly IPlayerService PlayerService;

        [ImportingConstructor]
        public LoginViewModel(IPlayerService PlayerService)
        {
            this.PlayerService = PlayerService;

            Initialize();
        }

        public void Initialize()
        {
            this.CurrPlayer = new Player();
            this.IsAuthParamsInvalid = false;

            this.LoginCommand = new DelegateCommand(Login);
        }

        void Login()
        {
            PlayerService.Login(CurrPlayer, a =>
                {
                    if (a.LoginSuccess)
                        EventAggregator.GetEvent<AuthorizationEvents.LoggedIn>().Publish();
                    else
                        this.IsAuthParamsInvalid = true;
                });
        }


        #region Bindable Properties
        public Player CurrPlayer
        {
            get { return mCurrPlayer; }
            set
            {
                if (mCurrPlayer == value) return;

                mCurrPlayer = value;
                FirePropertyChanged("CurrPlayer");
            }
        }
        private Player mCurrPlayer;

        public bool IsAuthParamsInvalid
        {
            get { return mIsAuthParamsInvalid; }
            set
            {
                if (mIsAuthParamsInvalid == value) return;

                mIsAuthParamsInvalid = value;
                FirePropertyChanged("IsAuthParamsInvalid");
            }
        }
        private bool mIsAuthParamsInvalid;
        #endregion

        #region Commands
        public ICommand LoginCommand { get; set; }
        #endregion

        #region Regions

        #endregion
    }
}